package com.wl.dzt.sys.controller;

import com.wl.dzt.sys.controller.action.Action;
import com.wl.dzt.sys.controller.param.Param;
import com.wl.dzt.sys.controller.param.shir.ShirBean;
import com.wl.dzt.sys.controller.param.shir.ShirHandler;
import com.wl.dzt.sys.controller.view.View;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @author wl_code@163.com
 * @version 1.0
 * @date 2018/7/5/005
 * @description 系统核心控制器
 */
@ApiIgnore //不对外开放
@Controller
@RequestMapping("$")
public class SupportController {

    @RequestMapping("handler")
    public String handler(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 构建参数
        Param param = Param.create(request);
        // 执行安全接口后构建交互
        String result = null;
        Action action = param.getAction();
        View view = param.getView();
        if (action != null && view != null) {
            request.setAttribute("param", param);
            ShirBean shirBean = param.getSupportHandlerInterceptor();
            if (shirBean != null && shirBean.isBefore() && !shirBean.doBefore(param)) {
                return null;
            }
            ShirHandler handler = new ShirHandler(request, response, view, action, param);
            if (shirBean != null && shirBean.isMiddle()) {
                result = shirBean.doMiddle(handler);
            } else {
                result = handler.invoke();
            }
            if (shirBean != null && shirBean.isAfter()) {
                shirBean.doAfter(param);
            }
            return result;
        }
        throw new Exception("未匹配到指定动作或者视图");
    }

}
